package com.lzx.shop.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lzx.shop.dao.ProductDetailDao;
import com.lzx.shop.dataobject.ProductDO;
import com.lzx.shop.dataobject.ProductDetailDO;
import com.lzx.shop.model.Result;
import com.lzx.shop.service.ProductDetailService;
import com.lzx.shop.service.ProductService;
import com.lzx.shop.utils.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 刘正星
 * @version 1.0.0
 * @ClassName ProductDetailApi.java
 * @Description TODO
 * @createTime 2021年03月21日 15:02:00
 */
@Api(tags = "ProductDetailApi",description = "商品详情管理")
@RestController
@RequestMapping("/productDetail")
public class ProductDetailApi {

    @Autowired
    private ProductDetailService productDetailService;

    @Autowired
    private ProductService productService;

    @ApiOperation("查询所有商品详情")
    @GetMapping("/list")
    public Result listCategory(){
        return ResultUtil.success(productDetailService.findAll());
    }

    @ApiOperation("根据商品id查询商品详情")
    @GetMapping("/get/{id}")
    public Result getById(@PathVariable("id")String id){
        if (StringUtils.isBlank(id)){
            return ResultUtil.paramError();
        }
        QueryWrapper<ProductDetailDO> queryWrapper = new QueryWrapper();
        queryWrapper.lambda().eq(ProductDetailDO::getProductId,id);
        List<ProductDetailDO> query = productDetailService.query(queryWrapper);

        query.forEach(productDetailDO -> {
            ProductDO productDO = productService.findById(productDetailDO.getProductId());
            productDetailDO.setProductDO(productDO);
        });
        return ResultUtil.success(query);
    }

    @ApiOperation("插入商品详情")
    @PostMapping("/save")
    public Result save(@RequestBody ProductDetailDO productDetailDO){
        if (productDetailDO==null){
            return ResultUtil.paramError();
        }

        return ResultUtil.success(productDetailService.save(productDetailDO));
    }

    @ApiOperation("更新商品详情")
    @PostMapping("/update")
    public Result update(@RequestBody ProductDetailDO productDetailDO){
        if (productDetailDO == null){
            return ResultUtil.paramError();
        }
        return  ResultUtil.success(productDetailService.update(productDetailDO));
    }

    @ApiOperation("删除商品详情")
    @DeleteMapping("/del/{id}")
    public Result delete(@PathVariable("id") String id){
        if (StringUtils.isBlank(id)){
            return ResultUtil.paramError();
        }
        return  ResultUtil.success(productDetailService.DeleteById(id));
    }
}
